{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb54955d",
   "metadata": {},
   "source": [
    "------\n",
    "* 学习人：林结婷\n",
    "* 时间：2021-12-6\n",
    "* week14\n",
    "* 学习线上资源：[python官方文档](https://www.python.org/)\n",
    "* 博客：[博客学习](https://www.cnblogs.com/xuzhichao/p/11570285.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28aab7e8",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "> 1. 函数的定义\n",
    "> 2. 函数名的定义规则-->变量名一样的规则\n",
    "> 3. 调用函数的方式\n",
    "> 4. 参数\n",
    ">> 1. 形式参数（定义函数）\n",
    ">> 2. 实际参数（调用参数）\n",
    ">> 3. 有多个参数时：\n",
    ">>> 0. 关键字参数\n",
    ">>> 1. 位置参数（调用函数时）---> 关键字参数要均放在末尾（多个不考虑顺序）\n",
    ">>> 2. 默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）\n",
    "## 本周内容\n",
    "> 1. 函数的返回值\n",
    "> 2. 函数的参数类型\n",
    "> 3. 模块与函数的关系\n",
    "> 4. 定义一个模块，然后调用模块\n",
    ">> 1. 调用模块\n",
    ">> 2. 调用模块中的函数\n",
    "> 5. 模块与函数实践\n",
    "## 1.函数的返回值\n",
    "> 1. 函数一定有返回值（无论是什么编程语言）\n",
    "> 2. 在其他的编程语言中（如c语言，java等），返回值一定要写，不写会报错！；对于python（更高级的语言），返回值如果不写，给定默认值 None\n",
    "> 3. 返回值一般为函数体运行的结果（result）（函数返回值实践1）;也可以是自定义的函数返回值(实践2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ca10494e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "59b99c26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "859f4533",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return 1\n",
    "    else:\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e24b3608",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36f30d70",
   "metadata": {},
   "source": [
    "## 函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d02feb7",
   "metadata": {},
   "source": [
    "### 1. 函数输入的参数\n",
    "> 1. 函数可以输入复杂的参数\n",
    ">> 1. 函数参数为列表\n",
    ">> 2. 函数参数为字典\n",
    "> 2. 函数可以输入不确定个数的参数：\n",
    ">> 1. *args\n",
    ">> 2. ** kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "549700fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "027069f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Xiaofeng', 'Ziqi']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Xiaofeng\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1cb15ee7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict_test(request_header):\n",
    "    \"\"\"测试输入参数为字典\"\"\"\n",
    "    print(request_header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b66959d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://httpbin.org/get\n",
    "httpbin_get = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61adb9bb-3c9ba8e966c3d5ad6c841e04\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.236\", \n",
    "  \"url\": \"http://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fcf1d225",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Host': 'httpbin.org', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36', 'X-Amzn-Trace-Id': 'Root=1-61adb9bb-3c9ba8e966c3d5ad6c841e04'}\n"
     ]
    }
   ],
   "source": [
    "dict_test(httpbin_get['headers'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76fe6aba",
   "metadata": {},
   "source": [
    "2. 函数可以输入不确定个数的参数：\n",
    "> 1. *args\n",
    ">> 1. args 可以接受多个数据（单值）\n",
    ">> 2. args 将多个数据存在一个元组当中，实际还是一个变量\n",
    "> 2. ** kwargs\n",
    ">> 1. kwargs 可以接受多个数据(键值对)==> 关键字参数\n",
    ">> 2. kwargs 将多个 键值对 的数据存放在一个 字典 中，实际上还是一个变量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de7ec41c",
   "metadata": {},
   "source": [
    "### 函数输出的返回值\n",
    "> 1. 返回值可以是 字符串、数值、列表、字典等数据（单一值）\n",
    "> 2. 返回值是唯一的，不可以有两个（这是其他语言，如C语言，Java等）\n",
    "> 3. python可以输出多个返回值，结果会用一个元组进行封装并返回。（本质不违背函数的定义）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "13f0137c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8bdc98e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4cf54c44",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c73e925d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "23857264",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return [1,2,3,4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d20525c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1e0556f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return httpbin_get['headers']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b193145b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',\n",
       " 'Accept-Encoding': 'gzip, deflate',\n",
       " 'Accept-Language': 'zh-CN,zh;q=0.9',\n",
       " 'Host': 'httpbin.org',\n",
       " 'Upgrade-Insecure-Requests': '1',\n",
       " 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',\n",
       " 'X-Amzn-Trace-Id': 'Root=1-61adb9bb-3c9ba8e966c3d5ad6c841e04'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "dcc76ff7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试多个值\n",
    "\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return\"\"\"\n",
    "    return 1,2,\"Lihua\",[1,2,3],{\"name\":\"Alex\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "10410a10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 'Lihua', [1, 2, 3], {'name': 'Alex'})"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40d3279c",
   "metadata": {},
   "source": [
    " > 1. 函数可以输入不确定个数的参数\n",
    " >> 1. *args\n",
    " >> 2. *kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "bd5a4cb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f923d8af",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'study' has no attribute 'search4vowels'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-29-44dad3c9eaf7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch4vowels\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"lihua\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'study' has no attribute 'search4vowels'"
     ]
    }
   ],
   "source": [
    "study.search4vowels(\"lihua\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10f1736a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
